//
//  LCTableView.m
//  YiJun
//
//  Created by Rochang on 2017/4/23.
//  Copyright © 2017年 Rochang. All rights reserved.
//

#import "LCTableView.h"

static NSString *cellId = @"cellId";

@interface LCTableView () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *maskView;
@property (weak, nonatomic) IBOutlet UIView *tipView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) NSArray <NSString *>*dataSource;

@end

@implementation LCTableView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tipView.layer.cornerRadius = 5;
    self.tipView.clipsToBounds = YES;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
}

+ (instancetype)shareInstance {
    static LCTableView *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    });
    return _instance;
}

- (void)showWihtTitle:(NSString *)title dataSource:(NSArray<NSString *> *)dataSource {
    self.dataSource = dataSource;
    self.titleLabel.text = title;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.tipView.alpha = 0;
    self.alpha = 0;
    self.tipView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tipView.alpha = 1.0;
        weakSelf.alpha = 1.0;
        weakSelf.tipView.transform = CGAffineTransformIdentity;
    }];
}

- (IBAction)close:(id)sender {
    [self hideWithAnimation:YES];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.text = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)hideWithAnimation:(BOOL)animtaion {
    if (!animtaion) {
        [self removeFromSuperview];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tipView.alpha = 0;
        weakSelf.alpha = 0;
        weakSelf.tipView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}


@end
